//npm install imagejs
//https://www.npmjs.com/package/imagejs#crop
//https://help.apple.com/app-store-connect/#/devd274dd925


https://www.cnblogs.com/yujidewu/p/7803675.html
var ImageJS = require("imagejs");

function calc_px(a,b){
	return Math.abs(parseInt((a-b)/2));
}

var filename='./src/splash.png';
var filename2='./src/splash2.png';
var filename3='./src/splash3.png';
//1倍
var width=2732;
var height=2732;
//2倍
var width2=1366;
var height2=1366;
//3倍
var width3=910;
var height3=910;

//缩放
var bitmap = new ImageJS.Bitmap();
bitmap.readFile(filename)
    .then(function() {
        // bitmap is ready
        bitmap.resize({
            width: 1366, height: 1366,
            algorithm: "nearestNeighbor"
        }).writeFile(filename2, { quality:100 })
            .then(function() {
                // bitmap has been saved
            });
        bitmap.resize({
            width: 910, height: 910,
            algorithm: "nearestNeighbor"
        }).writeFile(filename3, { quality:100 })
            .then(function() {
                // bitmap has been saved
            });    
    });

console.log('starting...');
setTimeout(()=>{
    var boxes=[
        [480,762],
        [720,1242],
        [1080,1882],
    ];
    for(let i=0;i<boxes.length;i++){
        let w,h,target_file;
        // 480*762
        w=boxes[i][0];
        h=boxes[i][1];
        var index=i+1;
        target_file='./dst/android/'+index+'-'+w+'@'+h+'.png';
        
        var s=2732/h;
        if(s>0 && s<=1){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>1 && s<=2){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>2 && s<=3){
            var _f=filename2;
            var _w=width2;
            var _h=height2;
        }else if(s>3){
            var _f=filename3;
            var _w=width3;
            var _h=height3;
        }else{
            console.log('error');
            return;
        }
        
        ((_f,_w,_h,w,h,target_file)=>{
            var bitmap = new ImageJS.Bitmap();
            bitmap.readFile(_f).then(function() {
                bitmap.crop({top: calc_px(_h,h), left: calc_px(_w,w), width: w, height: h})
                .writeFile(target_file, { quality:100 })
                .then(function() {
                    // bitmap has been saved
                    //console.log(_f);
                    console.log(w+'x'+h,'success!');
                    
                });
            });
        })(_f,_w,_h,w,h,target_file);
        
        
        
    }



    var boxes2=[
        [1242,2688],
        [828,1792],
        [1125,2436],
        [1242,2208],
        [750,1334],
        [640,1136],
        [640,960],
    ];
    for(let i=0;i<boxes2.length;i++){
        let w,h,target_file;
        // 480*762
        w=boxes2[i][0];
        h=boxes2[i][1];
        var index=i+1;
        target_file='./dst/iphone/'+index+'-'+w+'@'+h+'.png';
        
        var s=2732/h;
        if(s>0 && s<=1){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>1 && s<=2){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>2 && s<=3){
            var _f=filename2;
            var _w=width2;
            var _h=height2;
        }else if(s>3){
            var _f=filename3;
            var _w=width3;
            var _h=height3;
        }else{
            console.log('error');
            return;
        }
        
        ((_f,_w,_h,w,h,target_file)=>{
            var bitmap = new ImageJS.Bitmap();
            bitmap.readFile(_f).then(function() {
                bitmap.crop({top: calc_px(_h,h), left: calc_px(_w,w), width: w, height: h})
                .writeFile(target_file, { quality:100 })
                .then(function() {
                    // bitmap has been saved
                    //console.log(_f);
                    console.log(w+'x'+h,'success!');
                    
                });
            });
        })(_f,_w,_h,w,h,target_file);
        
        
        
    }


    var boxes3=[
        [2018,2732],
        [1668,2388],
        [1668,2224],
        [1536,2048],
        [768,1024],
    ];
    for(let i=0;i<boxes3.length;i++){
        let w,h,target_file;
        // 480*762
        w=boxes3[i][0];
        h=boxes3[i][1];
        var index=i+1;
        target_file='./dst/ipad/'+index+'-'+w+'@'+h+'.png';
        
        var s=2732/h;
        if(s>0 && s<=1){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>1 && s<=2){
            var _f=filename;
            var _w=width;
            var _h=height;
        }else if(s>2 && s<=3){
            var _f=filename2;
            var _w=width2;
            var _h=height2;
        }else if(s>3){
            var _f=filename3;
            var _w=width3;
            var _h=height3;
        }else{
            console.log('error');
            return;
        }
        
        ((_f,_w,_h,w,h,target_file)=>{
            var bitmap = new ImageJS.Bitmap();
            bitmap.readFile(_f).then(function() {
                bitmap.crop({top: calc_px(_h,h), left: calc_px(_w,w), width: w, height: h})
                .writeFile(target_file, { quality:100 })
                .then(function() {
                    // bitmap has been saved
                    //console.log(_f);
                    console.log(w+'x'+h,'success!');
                    
                });
            });
        })(_f,_w,_h,w,h,target_file);
        
        
        
    }
},3000);


	
